#include "faster.h"

extern vector<string> fileMap;

static pthread_t thread[MAXTHREAD];
//count锁
static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t fileMap_mutex = PTHREAD_MUTEX_INITIALIZER;
static int count = 0; //
static struct threadStatus myThreadPool[MAXTHREAD];

static void *processThread(void *)
{
    //int i = *((int *)param);
    pthread_mutex_lock(&counter_mutex);
    int i = count++;
    pthread_mutex_unlock(&counter_mutex);

    //cout << i << endl;
    struct threadStatus *pthread = &myThreadPool[i];
    //cout << myThreadPool[i].fileName <<endl;
    printf("thread[%d] start\n",i);

    while(1)
    {
        if(pthread->flag > 0)
        {
            vector<string> fileContent;
            //路径
            string temp = LOG_DIR +myThreadPool[i].fileName;
            string newname = NEW_DIR +myThreadPool[i].fileName;

            char host[100],username[100],pwd[100],db[50];
            MYSQL mysql;
            mysql_init(&mysql);

            fileToVector(temp.c_str(),fileContent);

            readFile(CONFIG,"host",host);
            readFile(CONFIG,"username",username);
            readFile(CONFIG,"password",pwd);
            readFile(CONFIG,"db",db);

            trim(host);
            trim(username);
            trim(pwd);
            trim(db);

            if(!mysql_real_connect(&mysql,host,username,pwd,db,0,NULL,0))
            {
                printf("thread[%d]Can't connect to mysqldb\n",i);
                break;
            }
            if (mysql_set_character_set(&mysql, "utf8"))
            {
                break;
            }

            for(vector<string>::iterator iter = fileContent.begin();iter != fileContent.end();++iter)
            {
                //insertLog(&mysql,temp.c_str(),(char *)((string)(*iter)).c_str());
                insertLog(&mysql,temp.c_str(),((string)(*iter)).c_str());
            }
            //move log files
            rename(temp.c_str(),newname.c_str());
            mysql_close(&mysql);
            pthread->flag = 0;
        }
        else
        {
            printf("sleep thread[%d]\n",i);
            sleep(1);
        }
    }

}

static void cleanThreadPool()
{
    for(int i = 0; i < MAXTHREAD;i++)
    {
        myThreadPool[i].flag = 0;
        myThreadPool[i].fileName = "";
        thread[i] = 0;
    }

}

static void creadThread()
{
    pthread_t temp;
    //这边直接创建不sleep计数器放在线程内部
    for(int i = 0; i < MAXTHREAD;i++)
    {
        if (pthread_create(&temp,NULL,processThread,(void *)NULL) > 0)
        {
            // printf("thread[%d] Ok",i);
        }
        //sleep(1);
    }
}

void run()
{
    cleanThreadPool();
    creadThread();
    pthread_mutex_destroy(&counter_mutex);
    while(1)
    {
        if (fileMap.size())
        {
            for(int i = 0;i < MAXTHREAD;i++)
            {
                if (myThreadPool[i].flag == 0)
                {

                    if (fileMap.size())
                    {

                        pthread_mutex_lock(&fileMap_mutex);
                        myThreadPool[i].fileName = fileMap.back();
                        fileMap.pop_back();
                        pthread_mutex_unlock(&fileMap_mutex);
                        myThreadPool[i].flag = 1;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
        }
        else
        {
            printf("main thread sleep 0.5 min\n");
            sleep(30);
            pthread_mutex_lock(&fileMap_mutex);
            readAllFiles(LOG_DIR);
            pthread_mutex_unlock(&fileMap_mutex);
        }
    }
}
